//
//  AYSVideoPlayerViewController.m
//  AiYingShiProject
//
//  Created by lanou on 15/10/30.
//  Copyright © 2015年 李晓理. All rights reserved.
//

#import "AYSVideoPlayerViewController.h"
#import "AYSPlayerViewController.h"
#import "AYSPlayersViewController.h"
@interface AYSVideoPlayerViewController () <UIWebViewDelegate>

//@property (nonatomic, strong) AYSPlayerViewController *playerVC;
@property(nonatomic, strong)AYSPlayersViewController *playerVC;
@property (nonatomic, assign) BOOL isPlay;
@property (nonatomic, assign) BOOL isCustom;

@end

@implementation AYSVideoPlayerViewController

- (void)viewWillAppear:(BOOL)animated
{
    if (_isPlay)
    {
//        [self.navigationController popViewControllerAnimated:YES];
        [self dismissViewControllerAnimated:YES completion:nil];
    }
}

- (void)webViewDidFinishLoad:(UIWebView *)webView
{
    NSString *lJs = @"document.documentElement.innerHTML";
    NSString *lHtml = [webView stringByEvaluatingJavaScriptFromString:lJs];
    NSLog(@"html内容:%@",lHtml);
//     NSString *lJs2 = @"(document.getElementsByTagName(\"video\")[0]).getElementsByTagName(\"source\")[0].src";  //qiyi
    
    // 优酷 风行 中国蓝TV PPS 腾讯 华数TV
    NSString *lJs2 = @"(document.getElementsByTagName(\"video\")[0]).src";  // youku,tudou,ku6 ,souhu
    NSString *lm3u8 = [webView stringByEvaluatingJavaScriptFromString:lJs2];
    NSLog(@"video source:%@",lm3u8);
    
    if (lm3u8.length > 0 && !_isPlay)
    {
        _isPlay = YES;
        if (_isCustom)
        {
//            _playerVC = [[AYSPlayerViewController alloc] init];
            _playerVC = [[AYSPlayersViewController alloc] initWithHTTPLiveStreamingMediaURL:[NSURL URLWithString:lm3u8]];
//            _playerVC.videoSource = lm3u8;
            _playerVC.mediaMArray = self.videoArray;
            _playerVC.row = self.row;
            [self presentViewController:_playerVC animated:YES completion:nil];
            [self.view removeFromSuperview];
            self.view = [[UIView alloc] init];
        }
        NSLog(@"html内容:%@",lHtml);
        NSLog(@"video source:%@",lm3u8);
    }
}

- (BOOL)useCustomPlayer:(NSString *)sender
{
    if ([sender isEqualToString:@"www.letv.com"])
    {
        return NO;
    }
    else if ([sender isEqualToString:@"www.funshion.com"])
    {
        return YES;
    }
    else if ([sender isEqualToString:@"tv.sohu.com"])
    {
        return NO;
    }
    else if ([sender isEqualToString:@"www.iqiyi.com"])
    {
        return NO;
    }
    else  if ([sender isEqualToString:@"v.youku.com"])
    {
        return YES;
    }
    else  if ([sender isEqualToString:@"v.qq.com"])
    {
        return NO;
    }
    else if([sender isEqualToString:@"v.pps.tv"])
    {
        return YES;
    }
    else if([sender isEqualToString:@"v.pptv.com"])
    {
        return NO;
    }
    else if ([sender isEqualToString:@"www.tudou.com"])
    {
        return YES;
    }
    else if ([sender isEqualToString:@"www.fun.tv"])
    {
        return YES;
    }
    else if ([sender isEqualToString:@"www.wasu.cn"])
    {
        return YES;
    }
    return NO;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib
    
//    [[UIDevice currentDevice] setValue:[NSNumber numberWithInteger:UIInterfaceOrientationLandscapeRight] forKey:@"orientation"];
    
    NSArray *array = [_url componentsSeparatedByString:@"/"];
    NSString *text = array[2];
    
    _isCustom = [self useCustomPlayer:text];
    UIWebView *myWeb = [[UIWebView alloc] initWithFrame:self.view.bounds];
    NSURL *url = [NSURL URLWithString:self.url];
    NSString *str = [NSString stringWithContentsOfURL:[NSURL URLWithString:self.url] encoding:NSUTF8StringEncoding error:nil];
    NSLog(@"%@", str);
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    [myWeb setDelegate:self];
    [myWeb loadRequest:request];
    
    if (_isCustom)
    {
        myWeb.hidden = YES;
    }
    else
    {
//        [[UIDevice currentDevice] setValue:[NSNumber numberWithInteger:UIInterfaceOrientationLandscapeRight] forKey:@"orientation"];
        UIButton *button = [UIButton buttonWithType:(UIButtonTypeSystem)];
        button.frame = CGRectMake(20, 30, 30, 30);
        [button setImage:[UIImage imageNamed:@"网页返回"] forState:(UIControlStateNormal)];
        [button addTarget:self action:@selector(webViewButton:) forControlEvents:(UIControlEventTouchUpInside)];
        [myWeb addSubview:button];
    }
    [self.view addSubview:myWeb];
}

- (void) webViewButton:(UIButton *)button
{
//    [[UIDevice currentDevice] setValue:[NSNumber numberWithInteger:UIInterfaceOrientationPortrait] forKey:@"orientation"];
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
